package com.example.myapplication;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.recyclerview.widget.RecyclerView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class TianqiAdapter extends RecyclerView.Adapter<TianqiAdapter.ViewHolder> {

    private final ArrayList<JSONObject> arrayList;

    public TianqiAdapter(ArrayList<JSONObject> arrayList) {

        this.arrayList = arrayList;

    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

        //在此处 写 每一个子项需要执行的操作
        JSONObject jsonObject = arrayList.get(position);
        try {
            holder.dateTextView.setText(jsonObject.getString("date"));
            holder.dateTextView.append(" " + jsonObject.getString("week"));
            holder.tmpTextView.setText(jsonObject.getString("tem2"));
            holder.tmpTextView.append("~" + jsonObject.getString("tem1") + " ℃");
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup group, int viewType) {
        View view = LayoutInflater.from(group.getContext()).inflate(R.layout.item_tianqi, group, false);
        return new ViewHolder(view);
    }

    @Override
    public int getItemCount() {

        return arrayList.size();

    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        private final AppCompatTextView dateTextView;
        private final AppCompatTextView tmpTextView;

        private ViewHolder(View view) {

            super(view);
            dateTextView = view.findViewById(R.id.dateTextView);
            tmpTextView = view.findViewById(R.id.tmpTextView);

        }

    }

}
